<template>
	<view class="layout">
		<!-- 导航栏 -->
		<view class="navbar">
			<view class="statusBar" :style="{ height: getStatusBarHeight() + 'px' }"></view>
			<view class="titleBar" :style="{ height: getTitleBarHeight() + 'px', marginLeft: getLeftIconLeft() + 'px' }">
				<navigator url="/pages/novel/search">
					<view class="search">
						<uni-icons class="icon" type="search" color="#888" size="18"></uni-icons>
						<text class="text">搜索</text>
					</view>
				</navigator>
				<text style="margin-left: 20rpx;">搜索</text>
			</view>
		</view>
		<!-- 填充区域 -->
		<view class="fill" :style="{ height: getNavBarHeight() + 'px' }"></view>

		<!-- 主体内容 -->
		<view class="content">
			<!-- 左侧标签栏 -->
			<view class="sidebar" :style="{ height: `calc(100vh - ${getNavBarHeight()}px)` }">
				<view
					v-for="(tab, index) in tabs"
					:key="index"
					class="tab"
					:class="{ active: activeTab === tab.value }"
					@click="switchTab(tab.value)"
				>
					{{ tab.label }}
				</view>
			</view>

			<!-- 右侧内容区域 -->
			<scroll-view class="main-content" scroll-y="true" style="height: 100%;">
				<!-- 推荐榜 -->
				<view v-if="activeTab === 'recommend'" class="ranking-list">
					<view class="ranking-item" v-for="(item, index) in recommendList" :key="index" @click="goRead(item.novelId)">
						<view class="cover">
							<image :src="item.image" mode="aspectFill"></image>
							<view class="rank-badge" :style="getRankBadgeStyle(index)">{{ index + 1 }}</view>
						</view>
						<view class="info">
							<view class="title">{{ item.novelName }}</view>
							<view class="author">{{ item.author }}</view>
							<view class="meta">
								{{ item.name }} · {{ item.status }} · {{ item.count / 10000 }}万字
							</view>
						</view>
					</view>
				</view>

				<!-- 完结榜 -->
				<view v-if="activeTab === 'complete'" class="ranking-list">
					<view class="ranking-item" v-for="(item, index) in completeList" :key="index" @click="goRead(item.novelId)">
						<view class="cover">
							<image :src="item.image" mode="aspectFill"></image>
							<view class="rank-badge" :style="getRankBadgeStyle(index)">{{ index + 1 }}</view>
						</view>
						<view class="info">
							<view class="title">{{ item.novelName }}</view>
							<view class="author">{{ item.author }}</view>
							<view class="meta">
								{{ item.name }} · {{ item.status }} · {{ item.count / 10000 }}万字
							</view>
						</view>
					</view>
				</view>

				<!-- 新书榜 -->
				<view v-if="activeTab === 'new'" class="ranking-list">
					<view class="ranking-item" v-for="(item, index) in newList" :key="index" @click="goRead(item.novelId)">
						<view class="cover">
							<image :src="item.image" mode="aspectFill"></image>
							<view class="rank-badge" :style="getRankBadgeStyle(index)">{{ index + 1 }}</view>
						</view>
						<view class="info">
							<view class="title">{{ item.novelName }}</view>
							<view class="author">{{ item.author }}</view>
							<view class="meta">
								{{ item.name }} · {{ item.status }} · {{ item.count / 10000 }}万字
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { getStatusBarHeight, getTitleBarHeight, getNavBarHeight, getLeftIconLeft } from '@/utils/system.js';
import { ApigetRankPage } from '../api/novel.js';

// 标签数据
const tabs = reactive([
	{ label: '推荐榜', value: 'recommend' },
	{ label: '完结榜', value: 'complete' },
	{ label: '新书榜', value: 'new' },
]);

// 当前选中的标签
const activeTab = ref('recommend');

// 分页参数
const pageNum = ref(1);
const size = ref(15);
const hasMoreData = ref(true); // 是否还有更多数据
const isFirstLoad = ref(true); // 是否是第一次加载

// 推荐榜数据
const recommendList = reactive([]);
// 完结榜数据
const completeList = reactive([]);
// 新书榜数据
const newList = reactive([]);

// 获取数据
const fetchMoreData = async () => {
	try {
		const data = await ApigetRankPage(activeTab.value, pageNum.value, size.value);

		if (pageNum.value === 1) {
			switch (activeTab.value) {
				case 'recommend':
					recommendList.length = 0;
					recommendList.push(...data);
					break;
				case 'complete':
					completeList.length = 0;
					completeList.push(...data);
					break;
				case 'new':
					newList.length = 0;
					newList.push(...data);
					break;
			}
		} else {
			// 后续加载，合并数据
			switch (activeTab.value) {
				case 'recommend':
					recommendList.push(...data);
					break;
				case 'complete':
					completeList.push(...data);
					break;
				case 'new':
					newList.push(...data);
					break;
			}
			if (data.length < size.value) {
				hasMoreData.value = false;
				uni.showToast({
					title: '没有更多数据了',
					icon: 'none',
					duration: 1000,
				});
			}
		}
	} catch (error) {
		console.error('数据加载失败:', error);
	}
};

// 切换标签
const switchTab = (tab) => {
	activeTab.value = tab;
	pageNum.value = 1;
	hasMoreData.value = true;
	fetchMoreData();
};

// 触底加载
const onReachBottom = () => {
	if (!hasMoreData.value) return; // 如果没有更多数据，直接返回

	pageNum.value++; // 增加页码
	fetchMoreData(); // 获取更多数据
};

// 获取排名标签样式
const getRankBadgeStyle = (index) => {
	if (index < 3) {
		return { backgroundColor: ['#ff6b6b', '#ffa502', '#ffd700'][index] };
	}
	return { backgroundColor: '#ccc' };
};

// 初始化加载数据
onMounted(() => {
	fetchMoreData();
});

// 跳转到阅读页面
const goRead = (val) => {
	uni.navigateTo({
		url: `/pages/novel/bookCover?bookId=${val}`,
	});
};
</script>

<style lang="scss" scoped>
.layout {
	display: flex;
	flex-direction: column;
	height: 100vh;
	background-color: #fff;

	.navbar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 10;
		background-color: #fff;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

		.titleBar {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;
			height: 88rpx;

			.search {
				width: 550rpx;
				height: 50rpx;
				border-radius: 60rpx;
				background: rgba(255, 251, 255, 0.4);
				border: 1px solid #eee;
				display: flex;
				align-items: center;
				justify-content: center;

				.icon {
					margin-right: 10rpx;
				}

				.text {
					color: #888;
					font-size: 28rpx;
				}
			}
		}
	}

	.fill {
		height: 88rpx;
	}

	.content {
		display: flex;
		height: calc(100vh - 108rpx);

		.sidebar {
			width: 150rpx;
			background-color: #f5f5f5;
			padding: 20rpx 0;
			box-shadow: 2px 0 10px rgba(0, 0, 0, 0.05);
			position: fixed;
			z-index: 5;

			.tab {
				padding: 20rpx 0;
				text-align: center;
				font-size: 28rpx;
				color: #666;
				cursor: pointer;

				&.active {
					color: #007aff;
					font-weight: bold;
					border-left: 4rpx solid #007aff;
					background-color: #fff;
				}
			}
		}

		.main-content {
			flex: 1;
			margin-left: 150rpx;
			padding: 20rpx;
			height: calc(100vh - 108rpx);
			overflow: hidden;
			background-color: #fff;

			.ranking-list {
				.ranking-item {
					display: flex;
					align-items: center;
					padding: 20rpx 0;
					border-bottom: 1px solid #f0f0f0;

					.cover {
						position: relative;
						width: 120rpx;
						height: 160rpx;
						margin-right: 20rpx;
						overflow: hidden;
						border-radius: 10rpx;

						image {
							width: 100%;
							height: 100%;
							object-fit: cover;
						}

						.rank-badge {
							position: absolute;
							top: 0;
							left: 0;
							width: 36rpx;
							height: 36rpx;
							color: white;
							font-size: 24rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							border-radius: 0 0 8rpx 0;
						}
					}

					.info {
						flex: 1;

						.title {
							font-size: 32rpx;
							font-weight: bold;
							margin-bottom: 10rpx;
							color: #333;
						}

						.author {
							font-size: 24rpx;
							color: #999;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							margin-bottom: 10rpx;
						}

						.meta {
							font-size: 24rpx;
							color: #999;
						}
					}
				}
			}
		}
	}
}
</style>